//
//  SNReturnGoodsRepetitiousCell.m
//  app
//
//  Created by 王少宁 on 15/5/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNReturnGoodsRepetitiousCell.h"
#import "SDPhotoBrowser.h"

@interface SNReturnGoodsRepetitiousCell ()<SDPhotoBrowserDelegate>
@property (nonatomic, retain) UIImageView * pointImgView;
@property (nonatomic, retain) UILabel * timeLabel;
@property (nonatomic, retain) UIImageView * leftTalkRectImgView;
@property (nonatomic, retain) UIImageView * startImgView;
@property (nonatomic, retain) UILabel * leftTalkLabel;
@property (nonatomic, retain) UIImageView * topTalkRectImgView;
@property (nonatomic, retain) UILabel * topTalkLabel;
@property (nonatomic, retain) UIView * picViewsBackGroundView;

/** 图片的原图*/
@property (nonatomic, retain) NSMutableArray * placeholderImageArray;
/** 高质量图片url数组*/
@property (nonatomic, retain) NSMutableArray * highQualityImageURLArray;

@end

@implementation SNReturnGoodsRepetitiousCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _pointImgView = CREATE_WITH_FRAME_OF(UIImageView, [self.contentView.ufo fx:0 ux:24 fy:0 oy:25],
                                             [self.contentView.ubo ow:10 oh:10]);
        _pointImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dot_gray@2x" ofType:@"png"]];
        [self.contentView addSubview:_pointImgView];
        
        _timeLabel = CREATE_WITH_FRAME_OF(UILabel, [_pointImgView.ufo fx:1.0 ox:10 fy:0 oy:-5],
                                          [self.contentView.ubo uw:-24 ow:SCREEN_WIDTH-10-_pointImgView.ufo.xx oh:20]);
        _timeLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
        _timeLabel.font = JKAppGlobleFontSmallTagText;
        [self.contentView addSubview:_timeLabel];
        
        _leftTalkRectImgView = [[UIImageView alloc] init];
        UIImage * image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"interlocutionRect_left@2x" ofType:@"png"]];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(UP(55), UP(40), UP(3), UP(3)) resizingMode:UIImageResizingModeStretch];
        _leftTalkRectImgView.image = image;
        [self.contentView addSubview:_leftTalkRectImgView];
        
        _leftTalkLabel = [[UILabel alloc] init];
        _leftTalkLabel.numberOfLines = 0;
        _leftTalkLabel.font = JKAppGlobleFontSmallTagText;
        _leftTalkLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
        [_leftTalkRectImgView addSubview:_leftTalkLabel];
        
        _startImgView = [[UIImageView alloc] init];
        _startImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"start_six_gray@2x" ofType:@"png"]];
        [_leftTalkRectImgView addSubview:_startImgView];
        
        self.picViewsBackGroundView = [[UIView alloc] init];
        [self.contentView addSubview:self.picViewsBackGroundView];
        self.placeholderImageArray = [NSMutableArray array];
        self.highQualityImageURLArray = [NSMutableArray array];
    }
    
    return self;
}

- (void)cellWithModel:(SNReturnGoodsAskAndRespondModel *)model row:(NSInteger)row
{
    self.row = row;
    [self.placeholderImageArray removeAllObjects];
    [self.highQualityImageURLArray removeAllObjects];
    _timeLabel.text = model.returnReqTime;
    NSMutableAttributedString * returnSeasonAttributedStr = [[NSMutableAttributedString alloc] initWithString:model.returnsReason];
    [returnSeasonAttributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} range:NSMakeRange(0, 7)];
    _leftTalkLabel.attributedText = returnSeasonAttributedStr;
    
    CGFloat leftTalkLabelHeight = [XCommon heightForString:model.returnsReason fontSize:13 andWidth:kLeftRectWidth];
    
    _leftTalkRectImgView.frame = CGRectOf([_timeLabel.ufo fx:0 fy:1.0 oy:10],
                                          [_timeLabel.ubo fw:1.0 oh:leftTalkLabelHeight+20]);
    _leftTalkLabel.frame = CGRectOf([_leftTalkRectImgView.ubo ox:20 oy:10],
                                    [self.contentView.ubo ow:SCREEN_WIDTH-50-UF(48) oh:leftTalkLabelHeight]);

    _startImgView.frame = CGRectOf([_leftTalkLabel.ufo fx:0 ox:-10 fy:0 oy:5],
                                   [_leftTalkLabel.ubo ow:5 oh:5]);
    
    if (model.reasonPic.count != 0) {
        
        self.picViewsBackGroundView.frame = CGRectMake(0, _leftTalkRectImgView.ufo.yy+10, SCREEN_WIDTH, 65);
        self.placeholderImageArray = (NSMutableArray *)model.reasonPic;
        
        for (int i = 0; i < model.reasonPic.count; i++) {
        
            NSString* str = model.reasonPic[i];
            str = [str stringByReplacingOccurrencesOfString:@"_\\w{2}\\d{3}" withString:@""
                                                    options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];
            
            [self.highQualityImageURLArray addObject:str];
            
#pragma mark - 添加图片
            UIImageView * picBtn = [[UIImageView alloc] init];
            picBtn.frame = CGRectMake(_leftTalkRectImgView.ufo.x+i*64+i*10, 0, 65, 65);
            picBtn.contentMode = UIViewContentModeScaleAspectFit;
            [picBtn sd_setImageWithURL:[NSURL URLWithString:model.reasonPic[i]]];
            picBtn.tag = 1 + i;
            picBtn.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picBtnClick:)];
            [picBtn addGestureRecognizer:tap];
            [self.picViewsBackGroundView addSubview:picBtn];

        }
    }
    
    if (model.isAdminProcess) {//有客服回复
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3.0f];//调整行间距
        CGFloat topTalkLabelHeith = [model.adminProcess boundingRectWithSize:CGSizeMake(kTopRectWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSParagraphStyleAttributeName:paragraphStyle} context:nil].size.height;
        
        _topTalkRectImgView = [[UIImageView alloc] init];
        UIImage * image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"respondRect@2x" ofType:@"png"]];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(UP(40), UP(55), UP(3), UP(2)) resizingMode:UIImageResizingModeStretch];
        _topTalkRectImgView.image = image;
        [self.contentView addSubview:_topTalkRectImgView];
        
        _topTalkLabel = [[UILabel alloc] init];
        _topTalkLabel.numberOfLines = 0;
        [_topTalkRectImgView addSubview:_topTalkLabel];
        
        NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:model.adminProcess];
        [attributeStr addAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} range:NSMakeRange(0, 7)];
        [attributeStr addAttributes:@{NSForegroundColorAttributeName:[XCommon hexStringToColor:kColor_l_light_gray_text],NSFontAttributeName:JKAppGlobleFontSmallTagText} range:NSMakeRange(6, model.adminProcess.length-6)];
        [attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.adminProcess length])];

        _topTalkLabel.attributedText = attributeStr;
        
        if (model.isPic) { //有图片
            _topTalkRectImgView.frame = CGRectOf([_leftTalkRectImgView.ufo fx:0 fy:1.0 oy:85],
                                                 [_leftTalkRectImgView.ubo fw:1.0 oh:topTalkLabelHeith+30]);
            _topTalkLabel.frame = CGRectOf([_topTalkRectImgView.ubo ox:10 oy:18],
                                           [_topTalkRectImgView.ubo fw:1.0 ow:-20 oh:topTalkLabelHeith]);
        } else { //没有图片
            _topTalkRectImgView.frame = CGRectOf([_leftTalkRectImgView.ufo fx:0 fy:1.0 oy:10],
                                                 [_leftTalkRectImgView.ubo fw:1.0 oh:topTalkLabelHeith+30]);
            _topTalkLabel.frame = CGRectOf([_topTalkRectImgView.ubo ox:10 oy:18],
                                           [_topTalkRectImgView.ubo fw:1.0 ow:-20 oh:topTalkLabelHeith]);
        }
    }
    
}

- (void)picBtnClick:(UITapGestureRecognizer *)tap
{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = self.picViewsBackGroundView; // 原图的父控件
    browser.imageCount = self.placeholderImageArray.count; // 图片总数
    browser.currentImageIndex = tap.view.tag - 1;
    browser.delegate = self;
    [browser show];
}

// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    if (self.placeholderImageArray.count > index) {
        UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.placeholderImageArray[index]]]];
        return image;
    } else {
        return nil;
    }
}


// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    if (self.highQualityImageURLArray.count > index) {
        return self.highQualityImageURLArray[index] ? self.highQualityImageURLArray[index] : nil;
    } else {
        return nil;
    }
}

- (void)drawRect:(CGRect)rect
{
    CGFloat x = _pointImgView.ufo.x+_pointImgView.ubo.width*0.5;
    if (self.row != 0) {
        [self drawSingleLineFromPoint:CGPointMake(x, 0) toPoint:CGPointMake(x, _pointImgView.ufo.y) color:nil];
    }
    [self drawSingleLineFromPoint:CGPointMake(x, _pointImgView.ufo.yy) toPoint:CGPointMake(x, rect.size.height) color:nil];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
